package org.hit.burkun.index.term;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.hit.burkun.db.DBHelper;
import org.hit.burkun.entities.Entity;

public class DBCollection {
	/**
	 * 获取数据库中存储的各个url对应关系
	 */
	protected static Map<String, Entity.TYPE> filedTypeMap = new HashMap<String, Entity.TYPE>();
	protected static Map<Entity.TYPE, String> entityTypeUrl = new HashMap<>();

	static {
		filedTypeMap.put("GO", Entity.TYPE.GENE_ONTOLOGY);
		filedTypeMap.put("HP", Entity.TYPE.HP_ONTOLOGY);
		filedTypeMap.put("DO", Entity.TYPE.DISEASE_ONTOLOGY);
		filedTypeMap.put("NCBI_Gene", Entity.TYPE.GENE_ID);
		filedTypeMap.put("UniProtKB", Entity.TYPE.UNIPORT_AC);
		filedTypeMap.put("HGNC", Entity.TYPE.HGNC);

		init();
	}

	private static void init() {
		DBHelper dbh = DBHelper.getServerData();
		dbh.connectionDB();
		String sqlStr = "select name, url_syntax from db_collection";
		Collection<String[]> res = dbh.queryData(sqlStr, 2);
		for (String[] item : res) {
			String name = item[0];
			String url = item[1];
			if (filedTypeMap.containsKey(name)) {
				entityTypeUrl.put(filedTypeMap.get(name), url);
			}
		}
		dbh.disconnection();
	}

	/**
	 * 
	 * @param type
	 *            Entity.TYPE实体类型，主要有ontology和UniproKB，geneid
	 * @return 返回一个url模式，如http://www.disease-ontology.org/term/[example_id]
	 */
	public static String getEntityUrl(Entity.TYPE type) {
		if (entityTypeUrl.containsKey(type)) {
			return entityTypeUrl.get(type);
		}
		return "#";
	}

}
